package com.service.scheduling.task;

import com.service.dao.constants.ConfigConstants;
import com.service.dao.model.CoinsPrice;
import com.service.dao.model.OtcOrderDetail;
import com.service.dao.model.SysConfig;
import com.service.scheduling.service.CoinsPriceService;
import com.service.scheduling.service.ConfigService;
import com.service.scheduling.service.OtcOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年12月16日</dd>
 * </dl>
 *
 * @author
 */

@Slf4j
@Component
public class OTCTask {

    @Autowired
    private OtcOrderDetailService otcOrderDetailService;

    @Autowired
    private CoinsPriceService coinsPriceService;

    @Autowired
    private ConfigService configService;

//    @Scheduled(cron = "0 10 0 * * ?")
    public void doScheduled() {
        log.info("OTC-doScheduled start");
        Date today = new Date();

        Date yesterday = DateUtils.addDays(today, -1);
        OtcOrderDetail otcOrderDetail = otcOrderDetailService.getLastDealOrder(yesterday);

//        Map<String, SysConfig> configMap = configService.mapByConfigGroup(ConfigConstants.otcPriceGroup());
//        CoinsPrice coinsPrice = coinsPriceService.getLastCoinsPrice(Integer.parseInt(configMap.get(ConfigConstants.OTC_COIN_TYPE_ID).getConfValue()));
//        BigDecimal maxPrice = coinsPrice.getMaxPrice();
//        BigDecimal minPrice = coinsPrice.getMinPrice();
//        BigDecimal price = coinsPrice.getCoinPrice();

        if (!ObjectUtils.isEmpty(otcOrderDetail)) {
//            price = otcOrderDetail.getOtcPrice();
//            maxPrice = otcOrderDetailService.getMaxPriceOrder(yesterday, otcOrderDetail.getCoinsId());
//            minPrice = otcOrderDetailService.getMinPriceOrder(yesterday, otcOrderDetail.getCoinsId());
        }

        CoinsPrice newCoinPrice = new CoinsPrice();
//        newCoinPrice.setCoinsId(coinsPrice.getCoinsId());
//        newCoinPrice.setCoinsName(coinsPrice.getCoinsName());
//        newCoinPrice.setCoinPrice(price);
//        newCoinPrice.setMaxPrice(maxPrice);
//        newCoinPrice.setMinPrice(minPrice);
        newCoinPrice.setCreateTime(today);
//        newCoinPrice.setStandardPrice(getStandardPrice(configMap));

        coinsPriceService.save(newCoinPrice);
        log.info("OTC-doScheduled end");

    }

    private BigDecimal getStandardPrice(Map<String, SysConfig> configMap) {
        BigDecimal basePrice = BigDecimal.valueOf(Double.parseDouble(configMap.get(ConfigConstants.EXTO_BASE_PRICE).getConfValue()));
        BigDecimal floatPrice = BigDecimal.valueOf(Double.parseDouble(configMap.get(ConfigConstants.EXTO_FLOAT_PRICE).getConfValue()));
        return basePrice.add(getFloatNum(floatPrice));
    }

    private BigDecimal getFloatNum(BigDecimal floatNum) {
        return BigDecimal.valueOf(RandomUtils.nextDouble(0, floatNum.doubleValue()));
    }

}

